import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
from rpy2.robjects import pandas2ri, default_converter
from rpy2.robjects.conversion import localconverter

# 导入 R 语言中的 summary() 函数和 psych 包
base = importr('base')
psych = importr('psych')
pandas2ri.activate()


def describe_analysis(df):
    """
    describe：描述性统计分析
    :param df:
    :return:
    """
    with localconverter(default_converter + pandas2ri.converter):
        # 将 DataFrame 对象转换为 R 语言的数据框
        dataframe = pandas2ri.py2rpy(df)
        # 对数据框进行描述性统计分析
        summary = base.summary(dataframe)
        describe = psych.describe(dataframe)
        summary = robjects.r['format'](summary, digits=4)
        describe = robjects.r['format'](describe, digits=4)
    return summary, describe
